"""登录模块"""

import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

from shopee.config import CF
from shopee.browser import browser, wait
from shopee.utils import *

LOGIN_URL = 'https://seller.shopee.com.my/account/signin'
LOGIN_COUNT = 0

browser.find_elements()


@TryExcept(not_raise=False)
@tip.register('login')
def login():
    global LOGIN_COUNT
    LOGIN_COUNT += 1
    log_print(f'Start of login {LOGIN_COUNT}')

    browser.get(LOGIN_URL)
    # try:
    #     cancel_button = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="shopee-modal__box"]/i')))
    #     cancel_button.click()
    # except:
    #     pass

    # 选择语言
    english_buttons_xpath = "//div[@class='language-selection__list']//button"
    english_buttons = browser.find_elements(By.XPATH, english_buttons_xpath)
    if len(english_buttons):
        browser.execute_script("arguments[0].click();", english_buttons[0])

    # 输入账号、密码
    login_email = wait.until(EC.presence_of_element_located((By.XPATH, '//input[@type="text"]')))
    login_password = browser.find_element(By.XPATH, '//input[@type="password"]')
    login_email.send_keys(CF.email)
    login_password.send_keys(CF.password)
    login_button = browser.find_elements(By.XPATH, '//form//button')[1]
    time.sleep(1)
    browser.execute_script("arguments[0].click();", login_button)

    print('Logging in, please wait.....')
    # 进度条
    short_rest(time_interval=0.15)
    print('Login succeeded!')
